#include <allegro.h>
#include <stdio.h>

struct Barres {
	BITMAP * img_vie[11];
	BITMAP * img_mana[9];
	BITMAP * base[1];
	int vie,vie_totale;
	int mana,mana_totale;
	int x=0;
	int y=0;
};
typedef struct Barres Barres;

struct Attaques
{
	int x,y,w,h;
	int atk=0;
	BITMAP * haut[4];
	BITMAP * bas[4];
	BITMAP * droite[4];
	BITMAP * gauche[4];
};
typedef struct Attaques Attaques;

void load_barres(Barres * P,char nom[])
{
	int i;
	char tmp[20];
	for (i = 0; i < 11; i++) 
	{
		sprintf(tmp, "./barres/vie/%d.bmp", i);
		P->img_vie[i] = load_bitmap(tmp, NULL);
	}
	for (i = 0; i < 9; i++) 
	{
		sprintf(tmp, "./barres/mana/%d.bmp", i);
		P->img_mana[i] = load_bitmap(tmp, NULL);
	}
	sprintf(tmp, "./barres/base/%s.bmp", nom);
	P->base[0] = load_bitmap(tmp, NULL);
}

void draw_barres(Barres P,BITMAP *Buffer)
{
	int percent_vie;
	int percent_mana;
	/* convertion des stats en pourcentage */
	percent_vie = 100 / ((P.vie_totale)/(P.vie)); 
	percent_mana = 100 / ((P.mana_totale)/(P.mana));
	/* affichage des barres */
	draw_sprite(Buffer, P.base[0] , P.x , P.y);
	   /* affichage de la vie */
	if(percent_vie == 0)
	{
		draw_sprite(Buffer, P.img_vie[0] , P.x , P.y);
	}
	else(percent_vie > 0 && percent_vie < 10)
	{
		draw_sprite(Buffer, P.img_vie[1] , P.x , P.y);
	}
	else(percent_vie >= 10 && percent_vie < 20)
	{
		draw_sprite(Buffer, P.img_vie[2] , P.x , P.y);
	}
	else(percent_vie >= 20 && percent_vie < 30)
	{
		draw_sprite(Buffer, P.img_vie[3] , P.x , P.y);
	}
	else(percent_vie >= 30 && percent_vie < 40)
	{
		draw_sprite(Buffer, P.img_vie[4] , P.x , P.y);
	}
	else(percent_vie >= 40 && percent_vie < 50)
	{
		draw_sprite(Buffer, P.img_vie[5] , P.x , P.y);
	}
	else(percent_vie >= 50 && percent_vie < 60)
	{
		draw_sprite(Buffer, P.img_vie[6] , P.x , P.y);
	}
	else(percent_vie >= 60 && percent_vie < 70)
	{
		draw_sprite(Buffer, P.img_vie[7] , P.x , P.y);
	}
	else(percent_vie >= 70 && percent_vie < 80)
	{
		draw_sprite(Buffer, P.img_vie[8] , P.x , P.y);
	}
	else(percent_vie >= 80 && percent_vie < 90)
	{
		draw_sprite(Buffer, P.img_vie[9] , P.x , P.y);
	}
	else(percent_vie >= 90 && percent_vie <= 100)
	{
		draw_sprite(Buffer, P.img_vie[10] , P.x , P.y);
	}	
	   /* affichage de la mana */
	if(percent_mana == 0)
	{
		draw_sprite(Buffer, P.img_mana[0] , P.x , P.y);
	}
	else(percent_mana > 0 && percent_mana < 12.5)
	{
		draw_sprite(Buffer, P.img_mana[1] , P.x , P.y);
	}
	else(percent_mana >= 12.5 && percent_mana < 25)
	{
		draw_sprite(Buffer, P.img_mana[2] , P.x , P.y);
	}
	else(percent_mana >= 25 && percent_mana < 37.5)
	{
		draw_sprite(Buffer, P.img_mana[3] , P.x , P.y);
	}
	else(percent_mana >= 37.5 && percent_mana < 50)
	{
		draw_sprite(Buffer, P.img_mana[4] , P.x , P.y);
	}
	else(percent_mana >= 50 && percent_mana < 62.5)
	{
		draw_sprite(Buffer, P.img_mana[5] , P.x , P.y);
	}
	else(percent_mana >= 62.5 && percent_mana < 75)
	{
		draw_sprite(Buffer, P.img_mana[6] , P.x , P.y);
	}
	else(percent_mana >= 75 && percent_mana < 87.5)
	{
		draw_sprite(Buffer, P.img_mana[7] , P.x , P.y);
	}
	else(percent_mana >= 87.5 && percent_mana <= 100)
	{
		draw_sprite(Buffer, P.img_mana[8] , P.x , P.y);
	}		
}

void destroy_barres(Barres *P)
{
	int i;
	for(i=0 ; i<11 ; i++)
	{
		destroy_bitmap(P->img_vie[i]);
	}
	for(i=0 ; i<9 ; i++)
	{
		destroy_bitmap(P->img_mana[i]);
	}
	destroy_bitmap(P->base[0]);
}

int calcul_degats(int attaque,int defense)
{
	int degats;
	if(attaque >= defense)
	{
		degats=(attaque-defense);
	}
	else
	{
		degats=0;
	}
}

int collision_persos(int sprite1_x, int sprite1_y, int sprite1_w, int sprite1_h, 
                      int sprite2_x, int sprite2_y, int sprite2_w, int sprite2_h)
{
	if ((sprite1_x > sprite2_x + sprite2_w - 1) || // is Sprite1 on the right side of Sprite2?
            (sprite1_y > sprite2_y + sprite2_h - 1) || // is b1 under b2?
            (sprite2_x > sprite1_x + sprite1_w - 1) || // is b2 on the right side of b1?
            (sprite2_y > sprite1_y + sprite1_h - 1)) // is b2 under b1?
    {
        // no collision
        return 0;
    }

    // collision
    return 1;
}

void load_attaques(Attaques * attaque)
{
	int i;
	
	char tmp[20];
	for (i = 0; i < 4; i++) 
	{
		sprintf(tmp, "./sprites/attaques/haut_%d.bmp", i);
		attaque->haut[i] = load_bitmap(tmp, NULL);
		
		sprintf(tmp, "./sprites/attaques/bas_%d.bmp", i);
		attaque->bas[i] = load_bitmap(tmp, NULL);
		
		sprintf(tmp, "./sprites/attaques/droite_%d.bmp", i);
		attaque->droite[i] = load_bitmap(tmp, NULL);
		
		sprintf(tmp, "./sprites/attaques/gauche_%d.bmp", i);
		attaque->gauche[i] = load_bitmap(tmp, NULL);
	}
}

void draw_attaques(BITMAP *Buffer,Attaques * attaque, Perso repere) /* pour copier les coordonnés du perso */
{
	int i;
	
	attaque->x = repere.x;
	attaque->y = repere.y;
	
	  /* attaque haut (dir=0) */
	
	if(Perso.direction == 0)
	{
		if(key[KEY_K])
		{
			attaque->atk = 1;
			for(i=0;i<4;i++)
			{
				draw_sprite(Buffer, attaque->haut[i] , attaque->x , attaque->y);
			}
			attaque->atk = 0;
		}
	}
	  
	  /* attaque bas (dir=1) */
	
	if(Perso.direction == 1)
	{
		if(key[KEY_K])
		{
			attaque->atk = 1;
			for(i=0;i<4;i++)
			{
				draw_sprite(Buffer, attaque->bas[i] , attaque->x , attaque->y);
			}
			attaque->atk = 0;
		}
	}
	
	  /* attaque droite (dir=2) */
	
	if(Perso.direction == 2)
	{
		if(key[KEY_K])
		{
			attaque->atk = 1;
			for(i=0;i<4;i++)
			{
				draw_sprite(Buffer, attaque->droite[i] , attaque->x , attaque->y);
			}
			attaque->atk = 0;
		}
	}
	
	  /* attaque gauche (dir=3) */
	
	if(Perso.direction == 3)
	{
		if(key[KEY_K])
		{
			attaque->atk = 1;
			for(i=0;i<4;i++)
			{
				draw_sprite(Buffer, attaque->gauche[i] , attaque->x , attaque->y);
			}
			attaque->atk = 0;
		}
	}
	
}

void hit(Barres * player, Barres * ennemi, Attaques player,Perso pplayer, Perso eennemi)
{
	int col=collision_persos(player.x,player.y,player.w,player.h,eennemi.x,eennemi.y,eennemi.w,eennemi.h);
	if (col==1)
	{
		if(player.atk==1)
		{
			int degats=calcul_degats(pplayer.attaque,eennemi.defense);
			&ennemi.vie-=degats;
			
		}
		if(player.atk==0)
		{
			int degats=calcul_degats(eennemi.attaque,pplayer.defense);
			&player.vie-=degats;
			
		}
	}
}
